package cn.taokuaidi.wangwang.client.widget.form;

import cn.taokuaidi.wangwang.client.context.AgentEventContext;
import cn.taokuaidi.wangwang.client.context.BuyerDataEventContext;
import cn.taokuaidi.wangwang.client.context.SellerDataEventContext;
import cn.taokuaidi.wangwang.client.data.Request;
import cn.taokuaidi.wangwang.client.model.AgentDO;
import cn.taokuaidi.wangwang.client.model.CommonResult;
import cn.taokuaidi.wangwang.client.model.OrderDO;
import cn.taokuaidi.wangwang.client.reader.CommonReader;
import cn.taokuaidi.wangwang.client.smart.AppConfig;
import cn.taokuaidi.wangwang.client.util.Screen;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseLoader;
import com.extjs.gxt.ui.client.data.HttpProxy;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;

public class ExpressForm extends TkdForm {

    protected TextField<String> no;
    protected ComboBox<AgentDO> combo;
    protected Window            self;
    protected OrderDO           order;
    private boolean             isSeller;

    /**
     * 提交数据
     */
    private void save() {
        if (no.getValue() == null || no.getValue().length() < 6) {
            MessageBox.alert("错误", "请检查快递号码是否填写正确，谢谢了!", null);
            return;
        }
        HttpProxy proxy = new HttpProxy(
                                        Request.saveNo(isSeller, order.getId(), combo.getValue().getId(), no.getValue()));
        BaseLoader loader = new BaseLoader(proxy, new CommonReader()) {

            @Override
            protected void onLoadSuccess(Object loadConfig, Object data) {
                super.onLoadSuccess(loadConfig, data);
                if (data instanceof CommonResult) {
                    CommonResult result = (CommonResult) data;
                    if (result.getFlag() == 1) {
                        self.setVisible(false);
                        // 从新加载数据
                        AppConfig appConfig = Screen.getAppConfig();
                        if (isSeller) {
                            SellerDataEventContext.getLoader().load(0, appConfig.sellerGridPageSize);
                        } else {
                            BuyerDataEventContext.getLoader().load(0, appConfig.buyerGridPageSize);
                        }
                    } else {
                        MessageBox.alert("错误", result.getMsg(), null);
                    }
                }
            }

        };
        loader.load();
    }

    public ExpressForm() {
        self = this;
        VerticalPanel vp = new VerticalPanel();
        vp.setSpacing(2);

        FormPanel simple = new FormPanel();
        simple.setFrame(true);
        simple.setHeaderVisible(false);

        combo = new ComboBox<AgentDO>();
        combo.setEditable(false);
        combo.setFieldLabel("快递公司");
        combo.setDisplayField("name");
        simple.add(combo);
        no = new TextField<String>();
        no.setFieldLabel("单号");
        simple.add(no);
        simple.setButtonAlign(HorizontalAlignment.CENTER);
        simple.addButton(new Button("保 存") {

            protected void onClick(ComponentEvent ce) {
                save();
            }

        });
        simple.addButton(new Button("取 消") {

            protected void onClick(ComponentEvent ce) {
                super.onClick(ce);
                self.setVisible(false);
            }

        });
        vp.add(simple);
        this.add(vp);
        this.setTitle("快递单设置");
        this.setHeaderVisible(true);
        this.setWidth(350);
    }

    @Override
    public void loadAgentData() {
        combo.setStore(AgentEventContext.getStore());

    }

    public void loadOrder(boolean isSeller, OrderDO order) {
        this.order = order;
        this.isSeller = isSeller;
        combo.setStore(AgentEventContext.getStore());
        if (order != null && order.getExpress() != null) {
            AgentDO currentAgent = order.getExpress().getAgent();
            combo.setValue(currentAgent);
            no.setValue(order.getExpress().getNo());
        } else {
            AgentDO currentAgent = AgentEventContext.getStore().getModels().get(0);
            combo.setValue(currentAgent);
            no.setValue(null);
        }

    }

    @Override
    public void loadOrder() {

    }

}
